package cn.itcast.sys.controller;


import cn.itcast.properties.MinioProperties;
import cn.itcast.result.Result;
import cn.itcast.utils.AliOssUtil;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

//    @Autowired
//    private MinioProperties minioProperties;
//    @PostMapping("/upload")
//    public Result upload(MultipartFile file){
//        log.info("文件上传：{}",file);
//
//        try{
//            String originalFilename = file.getOriginalFilename();
//            String fileName = originalFilename.substring(originalFilename.lastIndexOf('.'));
//            String newName = UUID.randomUUID().toString() + fileName;
//            String upload = aliOssUtil.upload(file.getBytes(), newName);
//            return Result.success(upload);
//        }catch (Exception e){
//            log.info("文件上传失败！");
//            return Result.error(MessageConstant.UPLOAD_FAILED);
//        }
//    }

    @Autowired
    MinioProperties minioProperties;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception{

//        MinioProperties minioProperties = new MinioProperties();
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint(minioProperties.getEndpoint())
                        .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                        .build();

        // Make 'asiatrip' bucket if not exist.
        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucket()).build());
        if (!found) {
            // Make a new bucket called 'asiatrip'.
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucket()).build());
        }

        UUID uuid = UUID.randomUUID();
        String s = file.getOriginalFilename();
        String objectName = uuid + s.substring(s.lastIndexOf("."));

        minioClient.putObject(
                PutObjectArgs.builder().bucket(minioProperties.getBucket()).object(objectName)
                        .stream(file.getInputStream(), file.getSize(), -1)
                        .contentType(file.getContentType())
                        .build());
//"https://" + minioProperties.getBucket() +"." +
        String url = minioProperties.getEndpoint() +"/" + minioProperties.getBucket() + "/" + objectName;
        return Result.success(url);
    }
}